package com.baiqian.doupo.event;

import com.baiqian.doupo.KeyBoradSave;
import com.baiqian.doupo.Utils;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Matrix3f;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class RenderEventHandler {
    //载入资源文件做贴图
    private static final ResourceLocation GetDouQi = new ResourceLocation(Utils.MOD_ID, "textures/particle/wispy.png");
    //通过渲染类型加载缓存
    private static final RenderType GetDouQiType = RenderType.getEntityTranslucent(GetDouQi);

    //RenderType.getEntityTranslucent(GUARDIAN_BEAM_LOCATION);//变成透明贴图模式
    //getEntityCutoutNoCull
    private static void func_229108_a_(
            IVertexBuilder vertexBuilder,//渲染图像的工具
            Matrix4f matrix4f,
            Matrix3f matrix3f,
            float x,
            float z,
            float y,
            int r,
            int g,
            int b,
            float u,
            float v) {
        vertexBuilder.pos(
                matrix4f,
                x,//以西东为x轴，其是x轴上的一点，单位为1方块
                z,//以下上为z轴，其是z轴上的一点，单位为1方块
                y//以北南为y轴，其是y轴上的一点，单位为1方块
        ).color(
                r,g,b,
                145
        ).tex(
                u,//相对于贴图左上角的位置x，此处采用百分比[0,1]
                v //相对于贴图左上角的位置y，此处采用百分比[0,1]
        ).overlay(OverlayTexture.NO_OVERLAY).lightmap(15728880).normal(
                matrix3f,
                0.0F,
                1F,
                0.0F).endVertex();
    }

    private static final float walk = 0.05f;//每次粒子移动的步长
    private static int lineNumMax = 2;//斗气丝带条数上限
    private static final ArrayList<Vector3d> linePoint = new ArrayList<>();
    private static float linePointRun = 1.0F;

    @SubscribeEvent
    public static void onRenderLivingEvent(RenderLivingEvent renderLivingEvent) {
        //只获取玩家实体，准备进行渲染
        if (renderLivingEvent.getEntity() instanceof PlayerEntity) {
            //获得实体
            LivingEntity entity = renderLivingEvent.getEntity();
            try {
                if (KeyBoradSave.keyG.get(entity.getUniqueID())>0) {

                    //获得渲染对象
                    MatrixStack matrixStack = renderLivingEvent.getMatrixStack();
                    //获得渲染缓冲区
                    IRenderTypeBuffer bufferIn = renderLivingEvent.getBuffers();
                    //得到实体的眼睛距离脚部的高度
                    float eyeHeight = entity.getEyeHeight();
                    //压栈渲染对象防止渲染污染
                    matrixStack.push();
                    //移动渲染原点至相对坐标(0,f3,0)【相对于脚部】
                    matrixStack.translate(0.0D, eyeHeight * 0.7, 0.0D);

                    //得到实体的位置矢量
                    Vector3d vector3d = entity.getPositionVec();//我自己
                    IVertexBuilder ivertexbuilder = bufferIn.getBuffer(GetDouQiType);
                    MatrixStack.Entry matrixstack$entry = matrixStack.getLast();
                    Matrix4f matrix4f = matrixstack$entry.getMatrix();
                    Matrix3f matrix3f = matrixstack$entry.getNormal();
                    //创建丝带
                    if (linePoint.size() < lineNumMax) {
                        linePoint.add(new Vector3d(Math.random() * 3 - 1.0f, Math.random() * 3 - 1.0f, Math.random() * 3 - 1.0f));
                    }
                    //渲染丝带
                    for (int i = 0; i < linePoint.size(); ++i) {
                        //求出我们之间的距离向量
                        Vector3d vector3d2 = linePoint.get(i);
                        //求向量的模，并+1.0D，表示贴图的横向长度
                        float length = (float) (vector3d2.length());
                        //求单位向量
                        vector3d2 = vector3d2.normalize();
                        //f5和f6共同作用:让渲染的模型指向目标
                        float f5 = (float) Math.acos(vector3d2.y);
                        float f6 = (float) Math.atan2(vector3d2.z, vector3d2.x);
                        matrixStack.rotate(Vector3f.YP.rotationDegrees((((float) Math.PI / 2F) - f6) * (180F / (float) Math.PI)));
                        matrixStack.rotate(Vector3f.XP.rotationDegrees(f5 * (180F / (float) Math.PI)));
                        //xz平面
                        func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.1F, length, -0.1F,
                                0, 0, 0, 1.0F, linePointRun);
                        func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.1F, 0.2F, -0.1F,
                                0, 0, 0, 1.0F, linePointRun - 1.0F);
                        func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.1F, 0.2F, -0.1F,
                                0, 0, 0, 0.0F, linePointRun - 1.0F);
                        func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.1F, length, -0.1F,
                                255, 255, 255, 0.0F, linePointRun);//颜色仅由最后一个决定
                        //减短丝带
                        double x = linePoint.get(i).x * 0.95;
                        double y = linePoint.get(i).y * 0.95;
                        double z = linePoint.get(i).z * 0.95;
                        linePoint.remove(i);
                        if (length <= 0.1F) {
                            --i;
                        } else {
                            linePoint.add(i, new Vector3d(x, y, z));
                        }
                    }
                    //动态丝带
                    if ((linePointRun += 0.002F) >= 1000.0F)
                        linePointRun = 1.0F;
                    lineNumMax = 3;
                    matrixStack.pop();
                }
            }catch (NullPointerException e){}
        }

    }
}

/*
*
    @SubscribeEvent
    public static void onRenderLivingEvent(RenderLivingEvent renderLivingEvent) {
        //只获取玩家实体，准备进行渲染
        if (renderLivingEvent.getEntity() instanceof PlayerEntity) {
            //获得实体
            LivingEntity entity = renderLivingEvent.getEntity();
            //获得渲染对象
            MatrixStack matrixStack = renderLivingEvent.getMatrixStack();
            //获得渲染缓冲区
            IRenderTypeBuffer bufferIn = renderLivingEvent.getBuffers();
            //获得已经渲染的时间
            float f = renderLivingEvent.getPartialRenderTick();
            //获得当前游戏时间
            float f1 = (float) entity.world.getGameTime();
            //得到实体的眼睛距离脚部的高度
            float f3 = entity.getEyeHeight();
            //压栈渲染对象防止渲染污染
            matrixStack.push();
            //移动渲染原点至相对坐标(0,f3,0)【相对于脚部】
            matrixStack.translate(0.0D, f3, 0.0D);
            //得到实体的位置矢量
            Vector3d vector3d = entity.getPositionVec();//我自己
            Vector3d vector3d1 = new Vector3d(vector3d.x + Math.random() * 4 - 2.0f, vector3d.y + Math.random() * 4 - 2.0f, vector3d.z + Math.random() * 4 - 2.0f);
            Vector3d vector3d2 = vector3d.subtract(vector3d1);//求出我们之间的距离向量
//            //求向量的模，并+1.0D，表示贴图的横向长度
//            float f4 = (float) (vector3d2.length());
//            //求单位向量
//            vector3d2 = vector3d2.normalize();
//            //f5和f6共同作用:让渲染的模型指向目标
//            //这可能是在把三维的单位长度转成二维下的仅仅是xy坐标系的xy轴的单位长度
//            float f5 = (float) Math.acos(vector3d2.y);
//            float f6 = (float) Math.atan2(vector3d2.z, vector3d2.x);
//            //应该是在计算三维模型扁平化映射到二维平面，依然是让渲染的模型指向目标，不加的话，特效就垂直上天
//            matrixStack.rotate(Vector3f.YP.rotationDegrees((((float) Math.PI / 2F) - f6) * (180F / (float) Math.PI)));
//            matrixStack.rotate(Vector3f.XP.rotationDegrees(f5 * (180F / (float) Math.PI)));

            IVertexBuilder ivertexbuilder = bufferIn.getBuffer(RenderType.getEntityTranslucent(GetDouQi,true));
            MatrixStack.Entry matrixstack$entry = matrixStack.getLast();
            Matrix4f matrix4f = matrixstack$entry.getMatrix();
            Matrix3f matrix3f = matrixstack$entry.getNormal();
            //func_229108_a_(ivertexbuilder, matrix4f, matrix3f, f19, f4, f20, j, k, l, 0.4999F, f30);

            //xz平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, 1.0F, -0.5F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, -1.0F, -0.5F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -1.0F, -0.5F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 1.0F, -0.5F,
                    0, 255, 0, 0.0F, 1.0F);//颜色仅由最后一个决定
            //xz平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, 1.0F, 0.5F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, -1.0F, 0.5F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -1.0F, 0.5F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 1.0F, 0.5F,
                    0, 255, 0, 0.0F, 1.0F);//颜色仅由最后一个决定

            //yz平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.5F, 1.0F, -1.0F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.5F, -1.0F, -1.0F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.5F, -1.0F, 1.0F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0.5F, 1.0F, 1.0F,
                    255, 0, 0, 0.0F, 1.0F);//颜色仅由最后一个决定
            //yz平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.5F, 1.0F, -1.0F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.5F, -1.0F, -1.0F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.5F, -1.0F, 1.0F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0.5F, 1.0F, 1.0F,
                    255, 0, 0, 0.0F, 1.0F);//颜色仅由最后一个决定

            //xy平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, -0.5F, -1.0F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, -0.5F, 1.0F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -0.5F, 1.0F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -0.5F, -1.0F,
                    0, 0, 255, 0.0F, 1.0F);//颜色仅由最后一个决定
            //xy平面
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, 0.5F, -1.0F,
                    0, 0, 0, 1.0F, 1.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, 0.5F, 1.0F,
                    0, 0, 0, 1.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 0.5F, 1.0F,
                    0, 0, 0, 0.0F, 0.0F);
            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 0.5F, -1.0F,
                    0, 0, 255, 0.0F, 1.0F);//颜色仅由最后一个决定

//            //xz平面
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, 1.0F, -0.0F,
//                    0, 0, 0, 1.0F, 1.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1F, -1.0F, -0.0F,
//                    0, 0, 0, 1.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -1.0F, 0.0F,
//                    0,  0,0, 0.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 1.0F, 0.0F,
//                    0, 255, 0, 0.0F, 1.0F);//颜色仅由最后一个决定
//
//            //yz平面
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0F, 1.0F, -1.0F,
//            0, 0, 0, 1.0F, 1.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -0F, -1.0F, -1.0F,
//                    0, 0, 0, 1.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0F, -1.0F, 1.0F,
//                    0, 0, 0, 0.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 0F, 1.0F, 1.0F,
//                    255, 0, 0, 0.0F, 1.0F);//颜色仅由最后一个决定
//
//            //xy平面
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, 0F, -1.0F,
//                    0, 0, 0, 1.0F, 1.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, -1.0F, -0F, 1.0F,
//                    0, 0, 0, 1.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, -0F, 1.0F,
//                    0, 0, 0, 0.0F, 0.0F);
//            func_229108_a_(ivertexbuilder, matrix4f, matrix3f, 1.0F, 0F, -1.0F,
//                    0, 0, 255, 0.0F, 1.0F);//颜色仅由最后一个决定
            matrixStack.pop();
        }
    }
* */